package com.example.oasystem.controller.mail;


import com.example.oasystem.config.ConstantProperties;
import com.example.oasystem.dao.attachment.AoaAttachmentListDaoOfJian;
import com.example.oasystem.dao.dept.AoaDeptDao;
import com.example.oasystem.dao.mail.AoaInMailListDaoOfSun;
import com.example.oasystem.dao.note.AoaNoteListDao;
import com.example.oasystem.dao.position.AoaPositionDao;
import com.example.oasystem.entity.attachment.AoaAttachmentListOfJian;
import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.mail.AoaInMailListOfSun;
import com.example.oasystem.entity.mail.AoaMailReciverOfSun;
import com.example.oasystem.entity.mail.AoaMailnumberOfl;
import com.example.oasystem.entity.note.AoaNoteList;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.position.AoaPosition;
import com.example.oasystem.entity.process.AoaAttachmentList;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaInMailListServiceOfSun;
import com.example.oasystem.service.AoaUserService;
import com.example.oasystem.utils.AliyunOSSUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class AoaInMailListControllerOfSun {

    @Autowired
    private AoaInMailListServiceOfSun aoaInMailListService;

    @Autowired
    private AoaInMailListDaoOfSun aoaInMailListDao;

    @Autowired
    private AoaUserService aoaUserService;
    @Autowired
    private AoaNoteListDao noteListDao;
    @Autowired
    private AoaDeptDao aoaDeptDao;
    @Autowired
    private AoaPositionDao aoaPositionDao;
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private ConstantProperties constantProperties;
    @Autowired
    private AoaAttachmentListDaoOfJian aoaAttachmentListDaoOfJian;

    @RequestMapping("/mail")
    public String zhanshi(HttpServletRequest request, Model model,
                          @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                          @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ) {
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        PageInfo<AoaInMailListOfSun> aoaInMailListOfSuns = aoaInMailListService.selectInboxNum(pageNum, pageSize, baseKey,userId);
        Page page=new Page(aoaInMailListOfSuns.getSize(), Math.toIntExact(aoaInMailListOfSuns.getTotal()),aoaInMailListOfSuns.getPages(),aoaInMailListOfSuns.getPageNum(),aoaInMailListOfSuns.isIsFirstPage(),aoaInMailListOfSuns.isIsLastPage());
        int noread=aoaInMailListDao.selectInboxNum("",userId).size();
        int push=aoaInMailListDao.selectOutboxNum("",userId).size();
        int nopush=aoaInMailListDao.selectDraftBoxNum("",userId).size();
        int rubbish=aoaInMailListDao.selectMailTrashCanNum("",userId).size();
        System.out.println("page mailmailmailmailmailmailmailmailmailmail= " + page);
        System.out.println("baseKey mailmailmailmailmailmailmailmailmail= " + baseKey);
        model.addAttribute("maillist",aoaInMailListOfSuns.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","zhanshipaging");
        model.addAttribute("noread",noread);
        model.addAttribute("push",push);
        model.addAttribute("nopush",nopush);
        model.addAttribute("rubbish",rubbish);
        model.addAttribute("title","收件箱");
        return "mail/mail";
    }

    @RequestMapping("/zhanshipaging")
    public String paging(HttpServletRequest request, Model model,
                         @RequestParam(value = "title", required = false, defaultValue = "收件箱") String title,
                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                         @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ) {
        System.out.println("进来了---------------------------------------");
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        PageInfo<AoaInMailListOfSun> aoaInMailListOfSuns = aoaInMailListService.selectInboxNum(pageNum, pageSize, baseKey,userId);
        Page page=new Page(aoaInMailListOfSuns.getSize(), Math.toIntExact(aoaInMailListOfSuns.getTotal()),aoaInMailListOfSuns.getPages(),aoaInMailListOfSuns.getPageNum(),aoaInMailListOfSuns.isIsFirstPage(),aoaInMailListOfSuns.isIsLastPage());
        int noread=aoaInMailListDao.selectInboxNum("",userId).size();
        int push=aoaInMailListDao.selectOutboxNum("",userId).size();
        int nopush=aoaInMailListDao.selectDraftBoxNum("",userId).size();
        int rubbish=aoaInMailListDao.selectMailTrashCanNum("",userId).size();
        System.out.println("page zhanshipagingzhanshipagingzhanshipagingzhanshipagingzhanshipaging= " + page);
        System.out.println("baseKey zhanshipagingzhanshipagingzhanshipagingzhanshipagingzhanshipaging= " + baseKey);
        model.addAttribute("maillist",aoaInMailListOfSuns.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","zhanshipaging");
        model.addAttribute("noread",noread);
        model.addAttribute("push",push);
        model.addAttribute("nopush",nopush);
        model.addAttribute("rubbish",rubbish);
        model.addAttribute("title",title);
        return "mail/mailbody";
    }

    @RequestMapping("/amail")
    public String outbox(HttpServletRequest request, Model model,
                         @RequestParam(value = "title", required = false, defaultValue = "收件箱")String title,
                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                         @RequestParam(value = "ter", required = false, defaultValue = "1") int ter,
                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                         @RequestParam(value = "val", defaultValue = "") String baseKey
    ) {

        System.out.println("ter rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr= " + ter);
        System.out.println("进来了分类分页++++++++++++++++++++++++++++++++"+request.getHeader("referer"));
        System.out.println("title 11111111111111111111111111111111111111111111= " + title);
        System.out.println("baseKey 11111111111111111111111111111111111111111111= " + baseKey);
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        PageInfo<AoaInMailListOfSun> aoaInMailListOfSuns =null;
        Page page=null;
        int noread=aoaInMailListDao.selectInboxNum("",userId).size();
        int push=aoaInMailListDao.selectOutboxNum("",userId).size();
        int nopush=aoaInMailListDao.selectDraftBoxNum("",userId).size();
        int rubbish=aoaInMailListDao.selectMailTrashCanNum("",userId).size();
        switch (title){
            case "收件箱":
                aoaInMailListOfSuns = aoaInMailListService.selectInboxNum(pageNum, pageSize, baseKey,userId);
                page=new Page(aoaInMailListOfSuns.getSize(), Math.toIntExact(aoaInMailListOfSuns.getTotal()),aoaInMailListOfSuns.getPages(),aoaInMailListOfSuns.getPageNum(),aoaInMailListOfSuns.isIsFirstPage(),aoaInMailListOfSuns.isIsLastPage());
                break;
            case "发件箱":
                aoaInMailListOfSuns = aoaInMailListService.selectOutboxNum(pageNum, pageSize, baseKey,userId);
                page=new Page(aoaInMailListOfSuns.getSize(), Math.toIntExact(aoaInMailListOfSuns.getTotal()),aoaInMailListOfSuns.getPages(),aoaInMailListOfSuns.getPageNum(),aoaInMailListOfSuns.isIsFirstPage(),aoaInMailListOfSuns.isIsLastPage());
                break;
            case "草稿箱":
                aoaInMailListOfSuns = aoaInMailListService.selectDraftBoxNum(pageNum, pageSize, baseKey,userId);
                page=new Page(aoaInMailListOfSuns.getSize(), Math.toIntExact(aoaInMailListOfSuns.getTotal()),aoaInMailListOfSuns.getPages(),aoaInMailListOfSuns.getPageNum(),aoaInMailListOfSuns.isIsFirstPage(),aoaInMailListOfSuns.isIsLastPage());
                break;
            case "垃圾箱":
                aoaInMailListOfSuns = aoaInMailListService.selectMailReciverTrashCanNum(pageNum, pageSize, baseKey,userId);
                page=new Page(aoaInMailListOfSuns.getSize(), Math.toIntExact(aoaInMailListOfSuns.getTotal()),aoaInMailListOfSuns.getPages(),aoaInMailListOfSuns.getPageNum(),aoaInMailListOfSuns.isIsFirstPage(),aoaInMailListOfSuns.isIsLastPage());
                break;
        }
        model.addAttribute("maillist",aoaInMailListOfSuns.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","amail");
        model.addAttribute("noread",noread);
        model.addAttribute("push",push);
        model.addAttribute("nopush",nopush);
        model.addAttribute("rubbish",rubbish);
        model.addAttribute("title",title);
        if (ter==1){
            return "mail/allmail";
        }else {
            return "mail/mailbody";
        }

    }



    @RequestMapping("/smail")
    public String chakan(HttpServletRequest request, Model model,
                         @RequestParam(value = "id", required = false, defaultValue = "0") long id,
                         @RequestParam(value = "title", required = false, defaultValue = "1")String title
                         ){
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        AoaInMailListOfSun aoaInMailListOfSun = aoaInMailListService.selectOneMail(id);
       if (title.contains("收件箱")){
           List<Long> ids=new ArrayList<>();
           ids.add(id);
           aoaInMailListService.updateMailInbox(1,9,9,ids,userId);
       }

        model.addAttribute("mail",aoaInMailListOfSun);
        model.addAttribute("title",title);
        return "mail/seemail";
    }

   @RequestMapping("/wmail")
   public String updateMail(HttpServletRequest request, Model model,
                            @RequestParam(value = "huifu", required = false, defaultValue = "")String huifu,
                            @RequestParam(value = "id", required = false, defaultValue = "0") long id,
                            @RequestParam(value = "title", required = false, defaultValue = "1")String title,
                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey
   ){

       AoaUser user = (AoaUser) request.getSession().getAttribute("user");
       Long userId = user.getUserId();
       PageInfo<AoaUser> aoaUserList = aoaUserService.selectAllDeptAndPosition(pageNum, pageSize, baseKey);
       List<AoaDept> aoaDeptList = aoaDeptDao.selectAll();
       List<AoaPosition> positions = aoaPositionDao.selectAllPositions();
       Page page=new Page(aoaUserList.getSize(), Math.toIntExact(aoaUserList.getTotal()),aoaUserList.getPages(),aoaUserList.getPageNum(),aoaUserList.isIsFirstPage(),aoaUserList.isIsLastPage());
       List<AoaTypeList> aoaTypeLists = aoaInMailListDao.selectTypeList();
       List<AoaStatusList> aoaStatusLists = aoaInMailListDao.selectStatusList();
       List<AoaUser> emplist=aoaUserList.getList();
       List<AoaMailnumberOfl> aoaMailnumberOfls = aoaInMailListService.selectMailNum(userId);
       AoaInMailListOfSun aoaInMailListOfSun = aoaInMailListService.selectOneMail(id);
       model.addAttribute("mail",aoaInMailListOfSun);
       model.addAttribute("mailnum", aoaMailnumberOfls);
       model.addAttribute("emplist",emplist);
       model.addAttribute("title",title);
       model.addAttribute("typelist",aoaTypeLists);
       model.addAttribute("statuslist",aoaStatusLists);
       model.addAttribute("deptlist",aoaDeptList);
       model.addAttribute("poslist",positions);
       model.addAttribute("page",page);
       model.addAttribute("baseKey",baseKey);
       model.addAttribute("url","mailUserPaging");
       model.addAttribute("id",huifu);
       return "mail/wirtemail";
   }

   @RequestMapping("/mailUserPaging")
   public String updateMailpaging(HttpServletRequest request, Model model,
                                  @RequestParam(value = "id", required = false, defaultValue = "0") long id,
                                  @RequestParam(value = "title", required = false, defaultValue = "")String title,
                                  @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                  @RequestParam(value = "baseKey", defaultValue = "") String baseKey
   ){
       PageInfo<AoaUser> aoaUserList = aoaUserService.selectAllDeptAndPosition(pageNum, pageSize, baseKey);
       List<AoaDept> aoaDeptList = aoaDeptDao.selectAll();
       List<AoaPosition> positions = aoaPositionDao.selectAllPositions();
       Page page=new Page(aoaUserList.getSize(), Math.toIntExact(aoaUserList.getTotal()),aoaUserList.getPages(),aoaUserList.getPageNum(),aoaUserList.isIsFirstPage(),aoaUserList.isIsLastPage());
       model.addAttribute("emplist",aoaUserList.getList());
       model.addAttribute("deptlist",aoaDeptList);
       model.addAttribute("poslist",positions);
       model.addAttribute("page",page);
       model.addAttribute("baseKey",baseKey);
       model.addAttribute("url","mailUserPaging");
        return "common/reciversOfJian";
}

    @RequestMapping("/pushmail")
    public String addMailReciver(
            HttpServletRequest request, Model model, MultipartFile filePath,
            @RequestParam(value = "content", required = false, defaultValue = "")String mailContent,
            @RequestParam(value = "mailType",required = false,defaultValue = "0")long mailType,
            @RequestParam(value = "mailStatusid",required = false,defaultValue = "0")long mailStatusId,
            @RequestParam(value = "mailId",required = false,defaultValue = "0")long mailId,
            @RequestParam(value = "inmail",required = false,defaultValue = "0")int inmail,
            @RequestParam(value = "inReceiver",required = false,defaultValue = "")String inReceiver,
            @RequestParam(value = "mailTitle",required = false,defaultValue = "")String mailTitle,
            @RequestParam(value = "ta",required = false,defaultValue = "")String ta,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ){
        System.out.println("mailTitle 0000000000000000000000000000= " + mailTitle);
        System.out.println("mailStatusId eeeeeeeeeeeeeeeeeeeeeeeeeeeee= " + mailStatusId);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        //这里是填入图片地址
        String uploadUrl =null;
        Long mailFileId=null;
        AoaAttachmentListOfJian aoaAttachmentListOfJian = new AoaAttachmentListOfJian();
        if (filePath!=null&&filePath.getSize()!=0) {
            uploadUrl = uploadFile(filePath, uploadUrl);//这个url就是path
            aoaAttachmentListOfJian.setAttachmentPath(uploadUrl);
            String s = uploadUrl.split("\\?")[0];
            int beginIndex = s.lastIndexOf("/");
            String attachmentName = s.substring(beginIndex + 1);
            String attachmentShuffix = attachmentName.split("\\.")[1];
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            aoaAttachmentListOfJian.setModel("aoa_director");
            aoaAttachmentListOfJian.setUserId(user.getUserId() + "");
            aoaAttachmentListOfJian.setUploadTime(sdf.format(new Date()));
            aoaAttachmentListOfJian.setAttachmentShuffix(attachmentShuffix);
            aoaAttachmentListOfJian.setAttachmentName(filePath.getOriginalFilename());
            aoaAttachmentListOfJian.setAttachmentType(filePath.getContentType());
            aoaAttachmentListOfJian.setAttachmentSize(filePath.getSize() + "");
            //这下面是插入将文件对象插入附件表
            aoaAttachmentListDaoOfJian.insert(aoaAttachmentListOfJian);
            //这个是mail_file_id
            mailFileId=aoaAttachmentListOfJian.getAttachmentId();
        }
        if (mailTitle.contains("【回复】")){
            System.out.println("mailTitle 11111111111111111111111111111111111111111111111= " + mailTitle);
            AoaInMailListOfSun aoaInMailListOfSun =null;
            if (ta.equals("发送")){
                System.out.println("发送++++++++++++++++++++++++++++++++++++++++++++++++");
                aoaInMailListOfSun = new AoaInMailListOfSun(null, mailContent, time, mailFileId, mailTitle, mailType, userId, inReceiver, mailStatusId, null, 0, 1, 0, null, null, null, null, 0, null, null, null);
                long mid = aoaInMailListService.addMail(aoaInMailListOfSun);
                String[] split = inReceiver.split(";");
                List<AoaUser> aoaUsers = aoaUserService.selectProcessShenuser(split);
                for (AoaUser aoaUser : aoaUsers) {
                    Long uid = aoaUser.getUserId();
                    AoaMailReciverOfSun aoaMailnumberOfSun = new AoaMailReciverOfSun(null, 0, mid, uid, 0, 0);
                    aoaInMailListService.addMailNum(aoaMailnumberOfSun);
                }
            }else if (ta.equals("存草稿")){
                System.out.println("草稿++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                aoaInMailListOfSun = new AoaInMailListOfSun(null, mailContent, time, mailFileId, mailTitle, mailType, userId, inReceiver, mailStatusId, null, 0, 0, 0, null, null, null, null, 0, null, null, null);
                aoaInMailListService.addMail(aoaInMailListOfSun);
            }

        }else if (mailTitle.contains("【转发】")){
            System.out.println("mailTitle 2222222222222222222222222222222222222222222= " + mailTitle);
            String[] split = inReceiver.split(";");
            List<AoaUser> aoaUsers = aoaUserService.selectProcessShenuser(split);
            for (AoaUser aoaUser : aoaUsers) {
                Long uid = aoaUser.getUserId();
                AoaMailReciverOfSun aoaMailnumberOfSun = new AoaMailReciverOfSun(null, 0, mailId, uid, 0, 0);
                aoaInMailListService.addMailNum(aoaMailnumberOfSun);
            }
        }else if(ta.contains("发送") && mailId != 0){
            AoaInMailListOfSun mail= new AoaInMailListOfSun(mailId, mailContent, time, mailFileId, mailTitle, mailType, userId, inReceiver, mailStatusId, null, 0, 1, 0, null, null, null, null, 0, null, null, null);
            aoaInMailListService.updateMail(mail);
            String[] split = inReceiver.split(";");
            List<AoaUser> aoaUsers = aoaUserService.selectProcessShenuser(split);
            for (AoaUser aoaUser : aoaUsers) {
                Long uid = aoaUser.getUserId();
                AoaMailReciverOfSun aoaMailnumberOfSun = new AoaMailReciverOfSun(null, 0, mailId, uid, 0, 0);
                aoaInMailListService.addMailNum(aoaMailnumberOfSun);
            }
        }else if (ta.equals("存草稿")&& mailId != 0){
            AoaInMailListOfSun mail= new AoaInMailListOfSun(mailId, mailContent, time, mailFileId, mailTitle, mailType, userId, inReceiver, mailStatusId, null, 0, 1, 0, null, null, null, null, 0, null, null, null);
            aoaInMailListService.updateMail(mail);
        }else  {
            System.out.println("新建邮件！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
            System.out.println("mailTitle 3333333333333333333333333333333333= " + mailTitle);
            AoaInMailListOfSun aoaInMailListOfSun =null;
            if (ta.equals("发送")){
                System.out.println("发送++++++++++++++++++++++++++++++++++++++++++++++++");
                aoaInMailListOfSun = new AoaInMailListOfSun(null, mailContent, time, mailFileId, mailTitle, mailType, userId, inReceiver, mailStatusId, null, 0, 1, 0, null, null, null, null, 0, null, null, null);
                long mid = aoaInMailListService.addMail(aoaInMailListOfSun);
                String[] split = inReceiver.split(";");
                List<AoaUser> aoaUsers = aoaUserService.selectProcessShenuser(split);
                for (AoaUser aoaUser : aoaUsers) {
                    Long uid = aoaUser.getUserId();
                    AoaMailReciverOfSun aoaMailnumberOfSun = new AoaMailReciverOfSun(null, 0, mid, uid, 0, 0);
                    aoaInMailListService.addMailNum(aoaMailnumberOfSun);
                }
            }else if (ta.equals("存草稿")){
                System.out.println("草稿++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
                aoaInMailListOfSun = new AoaInMailListOfSun(null, mailContent, time, mailFileId, mailTitle, mailType, userId, inReceiver, mailStatusId, null, 0, 0, 0, null, null, null, null, 0, null, null, null);
                aoaInMailListService.addMail(aoaInMailListOfSun);
            }
        }

        return "forward:/mail";
    }

    @RequestMapping("/alldelete")
     public String trashCan(Model model,HttpServletRequest request,
                            @RequestParam("title") String title,
                            @RequestParam("ids") List<Long> ids,
                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ){
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        if (title.contains("收件箱")){
            aoaInMailListService.updateMailInbox(9,9,1,ids,userId);
        }else if (title.contains("垃圾箱")){
            aoaInMailListService.deleteMail(userId,ids);
        }
        else {
            aoaInMailListService.updateMailOutbox(9,1,9,ids);
            System.out.println("已更改+++++++++++++++++++++++++++++++++");
        }
        model.addAttribute("title",title);
        model.addAttribute("ter",5);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        return "forward:/amail?ter=5";
    }

    @RequestMapping("/watch")
    public String updateread(Model model,HttpServletRequest request,
                             @RequestParam("title")String title,
                             @RequestParam("ids")List<Long> ids,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey){
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        if (title.contains("收件箱")){
            aoaInMailListService.updateMailInbox(1,9,9,ids,userId);
        }
        model.addAttribute("title",title);
        model.addAttribute("ter",5);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        return "forward:/amail?ter=5";
    }

    @RequestMapping("/star")
    public String updateStar(Model model,HttpServletRequest request,
                             @RequestParam("title")String title,
                             @RequestParam("ids")List<Long> ids,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey){
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
    if (title.contains("收件箱")){
            aoaInMailListService.updateMailInbox(9,1,9,ids,userId);
        }else{
            aoaInMailListService.updateMailOutbox(1,9,9,ids);
        }
        model.addAttribute("title",title);
        model.addAttribute("ter",5);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        return "forward:/amail?ter=5";
    }


    @RequestMapping("/refresh")
    public String huanyuan(Model model,HttpServletRequest request,
                           @RequestParam("title")String title,
                           @RequestParam("ids")List<Long> ids,
                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                           @RequestParam(value = "baseKey", defaultValue = "") String baseKey){
        System.out.println("进来了恢复@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        aoaInMailListService.updateReduction(user.getUserId(),ids);
        model.addAttribute("title",title);
        model.addAttribute("ter",5);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        return "forward:/amail?ter=5";
    }

    @RequestMapping("/mailtitle")
    public String chaxun(@RequestParam(value = "title", required = false, defaultValue = "收件箱")String title,
                        @RequestParam(value = "val", defaultValue = "") String val){
    return "forward:/amail?ter=5";

    }
    public String uploadFile(MultipartFile filePath, String uploadUrl) {
        try {
            if(null != filePath){
                String filename = filePath.getOriginalFilename();
                if(!"".equals(filename.trim())){
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(filePath.getBytes());
                    os.close();
                    filePath.transferTo(newFile);
                    //上传到OSS
                    uploadUrl = constantProperties.getHead() + aliyunOSSUtil.upload(newFile) + constantProperties.getEnd();
                }
            }
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return uploadUrl;
    }
    @ResponseBody
    @RequestMapping("/downMail")
    public void downMail(@RequestParam(value = "fileId",defaultValue = "0")int fileId,
                           HttpServletResponse response
                           ){
        AoaAttachmentList attachmentList=null;
        //查看中下载
        attachmentList = noteListDao.selectOneAttach(Long.valueOf(fileId));
        String str=attachmentList.getAttachmentPath();
        String path1=str.substring(48);
        String[] path2=path1.split("\\?");
        System.out.println(path2[0]);
        AliyunOSSUtil.download(path2[0],response);
    }
}
